#!/bin/bash
# log4r 本地 CI 镜像脚本 (Bash)
# 用于在本地模拟 CI 流水线执行

set -e

BURN_IN_ITERATIONS=${1:-3}
SKIP_BURN_IN=${SKIP_BURN_IN:-false}
RUN_BENCHMARK=${RUN_BENCHMARK:-false}

echo "🔍 log4r 本地 CI 流水线"
echo "━━━━━━━━━━━━━━━━━━━━━━━"

# 1. 格式检查
echo -e "\n📝 [1/6] 格式检查..."
cargo fmt --all -- --check
echo "✅ 格式检查通过"

# 2. Clippy 检查
echo -e "\n🔎 [2/6] Clippy 检查..."
cargo clippy --all-targets --all-features -- -D warnings
echo "✅ Clippy 检查通过"

# 3. 运行测试
echo -e "\n🧪 [3/6] 运行测试..."
cargo test --all-features --verbose
echo "✅ 测试通过"

# 4. Burn-in 循环
if [ "$SKIP_BURN_IN" != "true" ]; then
    echo -e "\n🔥 [4/6] Burn-in 循环 ($BURN_IN_ITERATIONS 次迭代)..."
    for i in $(seq 1 $BURN_IN_ITERATIONS); do
        echo "  🔥 迭代 $i/$BURN_IN_ITERATIONS"
        cargo test --all-features --quiet || exit 1
    done
    echo "✅ Burn-in 通过，未检测到 flaky 测试"
else
    echo -e "\n⏭️ [4/6] 跳过 Burn-in"
fi

# 5. 文档构建
echo -e "\n📚 [5/6] 文档构建..."
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
echo "✅ 文档构建通过"

# 6. 性能基准测试 (可选)
if [ "$RUN_BENCHMARK" = "true" ]; then
    echo -e "\n⚡ [6/6] 性能基准测试..."
    cargo bench --all-features -- --noplot
    echo "✅ 基准测试通过"
else
    echo -e "\n⏭️ [6/6] 跳过基准测试 (设置 RUN_BENCHMARK=true 启用)"
fi

echo -e "\n━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ 本地 CI 流水线通过!"
echo ""
echo "提示: 使用以下命令运行完整检查:"
echo "  ./scripts/ci-local.sh 10  # 10 次 burn-in"
echo "  RUN_BENCHMARK=true ./scripts/ci-local.sh"
